# coding: utf-8
from enum import Enum

from PySide6.QtCore import QLocale
from qfluentwidgets import QConfig, ColorConfigItem, OptionsValidator, OptionsConfigItem, \
    Theme, qconfig, ConfigSerializer, ConfigItem, BoolValidator

from .setting import CONFIG_DIR
from .utils import isWin11


class Language(Enum):
    """ Language enumeration """

    CHINESE_SIMPLIFIED = QLocale(QLocale.Language.Chinese, QLocale.Country.China)
    CHINESE_TRADITIONAL = QLocale(QLocale.Language.Chinese, QLocale.Country.HongKong)
    ENGLISH = QLocale(QLocale.Language.English)
    AUTO = QLocale()


class LanguageSerializer(ConfigSerializer):
    """ Language serializer """

    def serialize(self, language):
        return language.value.name() if language != Language.AUTO else "Auto"

    def deserialize(self, value: str):
        return Language(QLocale(value)) if value != "Auto" else Language.AUTO


class Config(QConfig):
    """ Config of application """
    themeColor = ColorConfigItem("QFluentWidgets", "ThemeColor", '#7117ea')
    language = OptionsConfigItem(
        "MainWindow", "Language", Language.AUTO, OptionsValidator(Language), LanguageSerializer(), restart=True)
    dpiScale = OptionsConfigItem(
        "QFluentWidgets", "DpiScale", "Auto", OptionsValidator([1, 1.25, 1.5, 1.75, 2, "Auto"]), restart=True)
    micaEnabled = ConfigItem("MainWindow", "MicaEnabled", isWin11(), BoolValidator())


cfg = Config()
cfg.themeMode.value = Theme.AUTO
qconfig.load(CONFIG_DIR / 'config.json', cfg)
